<?php
/* --------------------------------------------------------------
   xtc_check_categories_status.inc.php 2023-07-11
   Gambio GmbH
   http://www.gambio.de
   Copyright (c) 2023 Gambio GmbH
   Released under the GNU General Public License (Version 2)
   [http://www.gnu.org/licenses/gpl-2.0.html]
   --------------------------------------------------------------

   based on:
   (c) 2003 XT-Commerce - community made shopping http://www.xt-commerce.com ($Id: xtc_check_categories_status.inc.php 1009 2005-07-11 16:19:29Z mz $)

   Released under the GNU General Public License
   ---------------------------------------------------------------------------------------*/

function xtc_check_categories_status($categories_id) {

	if (!$categories_id)
		return 0;

	$categorie_query = "SELECT
	                                   parent_id,
	                                   categories_status
	                                   FROM ".TABLE_CATEGORIES."
	                                   WHERE
	                                   categories_id = '".(int) $categories_id."'";

	$categorie_query = xtc_db_query($categorie_query);
    
    $categorie_data = xtc_db_num_rows($categorie_query) ? xtc_db_fetch_array($categorie_query,
                                                                             true) : ['categories_status' => 0];
    if ($categorie_data['categories_status'] == 0) {
		return 1;
	} else {
		if ($categorie_data['parent_id'] != 0) {
			if (xtc_check_categories_status($categorie_data['parent_id']) >= 1)
				return 1;
		}
		return 0;
	}

}

function xtc_get_categoriesstatus_for_product($product_id) {

	$categorie_query = "SELECT
	                                   categories_id
	                                   FROM ".TABLE_PRODUCTS_TO_CATEGORIES."
	                                   WHERE products_id='".(int)$product_id."'";

	$categorie_query = xtc_db_query($categorie_query);

	while ($categorie_data = xtc_db_fetch_array($categorie_query, true)) {
		if (xtc_check_categories_status($categorie_data['categories_id']) >= 1) {
			return 1;
		} else {
			return 0;
		}
		echo $categorie_data['categories_id'];
	}

}
